package net.zjitc.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

public class BServlet implements Servlet {
    private ServletConfig servletConfig;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.servletConfig = servletConfig;
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //获取请求的相关信息
        String remoteHost = servletRequest.getRemoteHost();
        String username = servletRequest.getParameter("username");

        System.out.println(username+remoteHost);


        String servletName = servletConfig.getServletName();
        System.out.println(servletName);
        String contextPath = servletConfig.getServletContext().getContextPath();
        System.out.println(contextPath);
        //字节流方式
        //ServletOutputStream outputStream = servletResponse.getOutputStream();
        //outputStream.write(servletName.getBytes());


        //字符流方式
        PrintWriter writer = servletResponse.getWriter();
        writer.write(contextPath);
        writer.write(username+"---"+remoteHost);
        //两种方式任选一种，不要同时使用
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
